package com.itheima.controller;

import com.itheima.domain.mongo.UserState;
import com.itheima.manager.UserManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    //查询用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
       return userManager.findByPage(pageNum,pageSize);
    }

    //查询用户数据
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId){
        return userManager.findUserInfoById(userId);
    }

    //查看动态列表
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByCondition4Page(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            Integer state,
            Long uid){
        return userManager.findMovementByCondition4Page(uid,state,pageNum,pageSize);
    }

    //查看单个动态
    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable ObjectId movementId){
        return userManager.findMovementById(movementId);
    }

    //查看动态的评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            String messageID){
        return userManager.findMovementComemntByPage(messageID,pageNum,pageSize);
    }
    //动态通过
    @PostMapping("/manage/messages/pass")
    public ResponseEntity movementStaterPass(@RequestBody String[] ids) {
        return userManager.movementStaterPass(ids);
    }

    //动态拒绝
    @PostMapping("/manage/messages/reject")
    public ResponseEntity movementStaterReject(@RequestBody String[] ids) {
        return userManager.movementStaterReject(ids);
    }


    /**
     * 用户冻结
     * @param userState
     * @return
     */
    @PostMapping("/manage/users/freeze")
    public ResponseEntity userFreeze(@RequestBody UserState userState){
        return userManager.userFreeze(userState);

    }


    /**
     * 用户解冻
     * @param map
     * @return
     */
    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity userUnFreeze(@RequestBody Map<String,String> map){
        // 接收参数
        String id = map.get("userId");
        long userId = Long.parseLong(id);
        String reasonsForThawing = map.get("reasonsForThawing"); //解冻原因
        return userManager.userUnFreeze(userId,reasonsForThawing);


    }

}
